/**
 * 订单状态管理Store
 */
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import type { Order, OrderType, OrderListQuery, OrderListResponse } from '@/types/order'
import {
  getOrderList,
  getOrderDetail,
  createOrder,
  updateOrder,
  deleteOrder,
  getOrderTypeList,
  getOrderTypesByCategory,
  addPaymentRecord,
  getPaymentList,
  verifyPayment,
} from '@/api/order'

export const useOrderStore = defineStore('order', () => {
  // 订单列表
  const orderList = ref<Order[]>([])
  const total = ref(0)
  const currentPage = ref(1)
  const pageSize = ref(20)

  // 当前订单详情
  const currentOrder = ref<Order | null>(null)

  // 订单类型列表
  const orderTypes = ref<OrderType[]>([])

  // 加载状态
  const loading = reactive({
    list: false,
    detail: false,
    create: false,
    update: false,
    delete: false,
    types: false,
  })

  /**
   * 获取订单列表
   */
  const fetchOrderList = async (params: OrderListQuery) => {
    try {
      loading.list = true
      const response = await getOrderList(params)
      const data = response.data || response
      orderList.value = data.list
      total.value = data.total
      currentPage.value = data.page
      pageSize.value = data.limit
      return response
    } catch (error) {
      console.error('获取订单列表失败:', error)
      throw error
    } finally {
      loading.list = false
    }
  }

  /**
   * 获取订单详情
   */
  const fetchOrderDetail = async (id: number) => {
    try {
      loading.detail = true
      const response = await getOrderDetail(id)
      currentOrder.value = response.data || response
      return response
    } catch (error) {
      console.error('获取订单详情失败:', error)
      throw error
    } finally {
      loading.detail = false
    }
  }

  /**
   * 创建订单
   */
  const createNewOrder = async (data: any) => {
    try {
      loading.create = true
      const response = await createOrder(data)
      return response
    } catch (error) {
      console.error('创建订单失败:', error)
      throw error
    } finally {
      loading.create = false
    }
  }

  /**
   * 更新订单
   */
  const updateOrderInfo = async (id: number, data: Partial<Order>) => {
    try {
      loading.update = true
      const response = await updateOrder(id, data)
      return response
    } catch (error) {
      console.error('更新订单失败:', error)
      throw error
    } finally {
      loading.update = false
    }
  }

  /**
   * 删除订单
   */
  const deleteOrderInfo = async (id: number) => {
    try {
      loading.delete = true
      const response = await deleteOrder(id)
      return response
    } catch (error) {
      console.error('删除订单失败:', error)
      throw error
    } finally {
      loading.delete = false
    }
  }

  /**
   * 获取订单类型列表
   */
  const fetchOrderTypes = async (params?: { category?: number; status?: number }) => {
    try {
      loading.types = true
      const response = await getOrderTypeList(params)
      orderTypes.value = response.data || response
      return response
    } catch (error) {
      console.error('获取订单类型失败:', error)
      throw error
    } finally {
      loading.types = false
    }
  }

  /**
   * 根据订单大类获取类型
   */
  const fetchOrderTypesByCategory = async (category: number) => {
    try {
      loading.types = true
      const response = await getOrderTypesByCategory(category)
      return response.data || response
    } catch (error) {
      console.error('获取订单类型失败:', error)
      throw error
    } finally {
      loading.types = false
    }
  }

  /**
   * 新增支付记录
   */
  const addOrderPayment = async (data: any) => {
    try {
      const response = await addPaymentRecord(data)
      return response
    } catch (error) {
      console.error('新增支付记录失败:', error)
      throw error
    }
  }

  /**
   * 获取支付记录列表
   */
  const fetchPaymentList = async (orderId: number) => {
    try {
      const response = await getPaymentList(orderId)
      return response.data || response
    } catch (error) {
      console.error('获取支付记录失败:', error)
      throw error
    }
  }

  /**
   * 核实支付记录
   */
  const verifyOrderPayment = async (id: number) => {
    try {
      const response = await verifyPayment(id)
      return response
    } catch (error) {
      console.error('核实支付记录失败:', error)
      throw error
    }
  }

  /**
   * 重置当前订单
   */
  const resetCurrentOrder = () => {
    currentOrder.value = null
  }

  return {
    // 状态
    orderList,
    total,
    currentPage,
    pageSize,
    currentOrder,
    orderTypes,
    loading,

    // 方法
    fetchOrderList,
    fetchOrderDetail,
    createNewOrder,
    updateOrderInfo,
    deleteOrderInfo,
    fetchOrderTypes,
    fetchOrderTypesByCategory,
    addOrderPayment,
    fetchPaymentList,
    verifyOrderPayment,
    resetCurrentOrder,
  }
})
